<?php


namespace app\lib;

use app\admin\model\System as SystemModel;
use WeChat\Pay;
use WeChat\Qrcode;
use WeMini\Crypt;
use WePay\Refund;

class MiniWechat
{
    public $config;

    public function __construct($companyId = 1)
    {
        $this->config = [
            'token'          => '',
            // 'appid'          => 'wx09280ebcd19ff489',
            //'appsecret'      => '3654c0f33e6fa7b5f8ef6d47599c3fd8',
            'appid'          => 'wx4afd3c5def11dcf5',
            'appsecret'      => 'c6bb648979b3480dab226c73c6c73200',
//            'encodingaeskey' => $company->encodingaeskey,
//            // 配置商户支付参数（可选，在使用支付功能时需要）
            'mch_id'         => '1627251397',
            'mch_key'        => '3654c0f33e6fa7b5f8ef6d47599c3fd8',
//            // 配置商户支付双向证书目录（可选，在使用退款|打款|红包时需要）
            'ssl_key'        => root_path() . 'app/lib//apiclient_key.pem',
            'ssl_cer'        => root_path() . 'app/lib//apiclient_cert.pem',
            // 缓存目录配置（可选，需拥有读写权限）
            'cache_path'     => '',
        ];
    }

    public function code2Session($jsCode)
    {
        $crypt = new Crypt($this->config);
        return $crypt->session($jsCode);
    }

    public function decode(...$params)
    {
        $crypt = new Crypt($this->config);
        return $crypt->decode(...$params);
    }

    public function createOrder($options)
    {
        $wechat = new Pay($this->config);
        // 生成预支付码
        $result = $wechat->createOrder($options);
        // 创建JSAPI参数签名
        return $wechat->createParamsForJsApi($result['prepay_id']);
    }

    public function refund($options)
    {
        $refund = new Refund($this->config);
        return $refund->create($options);
    }

    public function getNOtify()
    {
        $wechat = new Pay($this->config);
        return  $wechat->getNotify();
    }

    public function getNotifySuccessReply()
    {
        $wechat = new Pay($this->config);
        return  $wechat->getNotifySuccessReply();
    }

    public function createQrCode($scene, $page)
    {
        $wechat = new \WeMini\Qrcode($this->config);
        return $wechat->createMiniScene($scene, $page);
    }
}